#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>
#include <QTcpSocket>
#include "action.h"
#include "layout.h"
#include "actionsettingsdialog.h"
#include <QtGui>
#include <QDomDocument>
#include "relaylamp.h"

QT_BEGIN_NAMESPACE
class QLabel;
QT_END_NAMESPACE
class CircleWidget;

#define DEF_INI_FILENAME    "cuberemote-v2.ini"

enum ModeType
{
    MODE_GRIDVIEW,
    MODE_MAPVIEW
};

struct SocketServer {
    QString mAddress;
    QString mPortNum;
};

struct UserInfo {
    QString mUserId;
    QString mPassword;
};

struct ModeInfo {
    QString mLayoutImg;
    ModeType mLayoutMode;
    QString mLampPosFile;
    int mLampRadius;
    int mFontsize;
    bool mFontbold;
    bool mSubMenuOption;
};

//! [0]
class Window : public QGraphicsView
{
    Q_OBJECT

public:
    Window(QGraphicsScene *scene);
    ~Window();

protected:
    virtual void timerEvent(QTimerEvent *e);
    void wheelEvent(QWheelEvent *event);
    void resizeEvent(QResizeEvent *event)
    {
        QGraphicsView::resizeEvent(event);
        fitInView(sceneRect(), Qt::IgnoreAspectRatio);
    }
    void scaleView(qreal scaleFactor);

private:
    virtual bool winEvent(MSG *message, long *result);

private:
    void loadConfig(const QString fileName);
    void saveConfig(const QString fileName);
    void initContextMenu();
    void initWidget();
    void initTcpSocket();

    void initLayout();
    void updateStatus(int cid, int bid, int rid, int status, uint stime, uint etime);
    bool loadLampPos(const QString fileName);

Q_SIGNALS:
    void signalNeedToShow();

public Q_SLOTS:
    void onParseArgs(const QString& args);
    void onShowContextMenu(const QPoint& pos);
    void onMenuRelayOff();
    void onMenuRelayOn();
    void onMenuRelayAir();
    void onMenuRelayCooling();
    void onMenuRelayHeating();
    void onMenuSocketServer();
    void onMenuSync();
    void onMenuReboot();
    void onMenuActionViewMode();
    void onMenuActionSetting();
    void onMenuOptionCooling();
    void onMenuOptionHeating();
    void onMenuExit();
    void onButtonClicked();
    void onButtonClicked2();
    void onActionSettingsDialogShow(bool show);
    void hoverOnButton();
    void hoverOutButton();

    void onTcpConnect();
    void onTcpConnected();
    void onTcpRead();
    void onTcpConnectionClosedByServer();
    void onTcpError(QAbstractSocket::SocketError socketError);

private:
    int mOneSecondTimerId;

    SocketServer mSocketServer;
    UserInfo mUserInfo;
    QTcpSocket *mpTcpSoceket;
    bool mIsSocketInit;
    QString mTcpSoceketRecv;
    ModeInfo mModeInfo;

    Layout mLayout;
    Action mAction;

    ActionSettingsDialog *mActionSettingsDialog;

    QGraphicsPixmapItem* mBgItem;
    QGraphicsScene *mScene;
    QList<RelayLamp> mRelayLamp;
};

#endif
